package com.gd.luxiaoyu.moduels.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.luxiaoyu.Constant;
import com.gd.luxiaoyu.moduels.entity.UserTableEntity;
import com.gd.luxiaoyu.moduels.service.UserTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/*
* 用户控制类
*
* */
@RequestMapping("UserPage/user")
@Controller
public class UserTableController {

    @Autowired(required = true)
    private UserTableService userTableService;
    Constant constant =new Constant();
/*查询有的用户
* */
    @RequestMapping("page")
    @ResponseBody
    public Map<String,Object> page(Page<UserTableEntity> userPage, UserTableEntity user){

        IPage<UserTableEntity> userIPage =  userTableService.getUserByUsername(userPage,user);
        Map<String,Object> map =new HashMap<>();
        map.put("toals",userIPage.getTotal());
        map.put("rows",userIPage.getRecords());
        return map;
    }


    /*
    用户通过id修改自己的基本信息
    * */
    @PostMapping("update")//poset请求
    @ResponseBody
    public R<?> UpdateById(UserTableEntity userTableEntity){
        boolean update = userTableService.updateById(userTableEntity);
        return  update ? R.ok(constant.SUCCESSFUL) : R.failed(constant.FAIL);
    }
    /*
    * 管理员删除或者用户注销操作
    * */
    @RequestMapping("delete")
    @ResponseBody
    public R<?> DeleteById(UserTableEntity userTableEntity){
        boolean delete = userTableService.removeById(userTableEntity.getId());
        return  delete ? R.ok(constant.SUCCESSFUL) : R.failed(constant.FAIL);
    }

    /*
    * 插入新的用户
    * */
    @RequestMapping(value = "login/save")
    @ResponseBody
    public R<?> SaveUser(UserTableEntity userTableEntity){
        boolean save = userTableService.save(userTableEntity);
        return save ? R.ok(constant.SUCCESSFUL) : R.failed(constant.FAIL);
    }

    @RequestMapping("queryById")
    @ResponseBody
    public  UserTableEntity queryById(UserTableEntity userTableEntity){
        UserTableEntity user = userTableService.getById(userTableEntity.getUaccount());
        return user;
    }
    /*@RequestMapping("QAll")
    @ResponseBody
    public QueryChainWrapper<UserTableEntity> QAll(){
        QueryChainWrapper<UserTableEntity> user =  userTableService.query();
        System.out.println(user);
        return user;
    }*/

}
